/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerAgendarConsulta;
import controller.ControllerDentista;
import controller.ControllerPaciente;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;
import model.AgendarConsulta;
import model.Dentista;
import model.Login;
import model.Paciente;
import util.MascaraUtil;
import util.MensagemUtil;

/**
 *
 * @author Root
 */
public class ViewGuia extends javax.swing.JFrame {
    
    private ViewConsultarConsultasGuia view;
    private final ControllerPaciente controllerPaciente;
    private final ControllerDentista controllerDentista;
    private final ControllerAgendarConsulta controllerAgendarConsulta;
    private AgendarConsulta agendar;
    private final Dentista dentista;
    private final Paciente paciente;
    private Login login;
    
    public ViewGuia() {
        this(new Login());
    }
    
    public ViewGuia(Login l) {
        this.login = l;
        controllerAgendarConsulta = new ControllerAgendarConsulta();
        controllerDentista = new ControllerDentista();
        controllerPaciente = new ControllerPaciente();
        agendar = new AgendarConsulta();
        dentista = new Dentista();
        paciente = new Paciente();
        initComponents();
        lblLogado.setText(login.getPerfil().getDescricao());
        carregarComboDentista();
        carregarComboPaciente();
        carregarComboHora();
        btnExcluir.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        comboDentista = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        comboPaciente = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        botaoAprovar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaProcedimento = new javax.swing.JTextArea();
        dateChoose = new com.toedter.calendar.JDateChooser();
        lblDataConsulta = new javax.swing.JLabel();
        comboHora = new javax.swing.JComboBox();
        lblHoraConsulta = new javax.swing.JLabel();
        btnConsultar = new javax.swing.JToggleButton();
        btnSair = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        lblusuarioLogado = new javax.swing.JLabel();
        lblLogado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Dentista:");

        jLabel2.setText("Paciente:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("         Guia de Consulta");

        jLabel6.setText("Procedimento:");

        botaoAprovar.setText("Aprovar/Alterar");
        botaoAprovar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAprovarActionPerformed(evt);
            }
        });

        txtAreaProcedimento.setColumns(20);
        txtAreaProcedimento.setRows(5);
        jScrollPane1.setViewportView(txtAreaProcedimento);

        lblDataConsulta.setText("Data Consulta:");

        comboHora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboHoraActionPerformed(evt);
            }
        });

        lblHoraConsulta.setText("Hora Consulta:");

        btnConsultar.setText("Consultar Consultas Guia");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        lblusuarioLogado.setText("Usuario logado:");

        lblLogado.setText("-");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botaoAprovar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblDataConsulta)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(comboPaciente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(114, 114, 114))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(comboDentista, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(dateChoose, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(lblHoraConsulta)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(comboHora, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(0, 61, Short.MAX_VALUE)))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(135, 135, 135))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnConsultar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSair)
                                .addGap(13, 13, 13))))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblusuarioLogado)
                .addGap(18, 18, 18)
                .addComponent(lblLogado, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblusuarioLogado, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblLogado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboDentista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dateChoose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDataConsulta)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(comboHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblHoraConsulta)
                        .addComponent(btnExcluir)))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoAprovar)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConsultar)
                    .addComponent(btnSair))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoAprovarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAprovarActionPerformed
        salvarConsulta();
    }//GEN-LAST:event_botaoAprovarActionPerformed

    private void comboHoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboHoraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboHoraActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        ViewConsultarConsultasGuia gui = new ViewConsultarConsultasGuia(login);
        gui.setVisible(true);
        this.setVisible(false);

    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        deletar();
    }//GEN-LAST:event_btnExcluirActionPerformed
    
    private void deletar() {
        int status = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja deletar a consulta", "Confirmação", JOptionPane.YES_NO_OPTION);
        if (status == 0) {
            controllerAgendarConsulta.deletar(agendar);
            JOptionPane.showMessageDialog(null, "Consulta excluida com sucesso!!!");
            view = new ViewConsultarConsultasGuia(login);
            view.setVisible(true);
            this.setVisible(false);
        }
    }
    
    private void carregarDadosConsulta() {
        agendar.setDentista((Dentista) comboDentista.getSelectedItem());
        agendar.setPaciente((Paciente) comboPaciente.getSelectedItem());
        MascaraUtil.limitarCampoTextArea(txtAreaProcedimento, "Procedimento", 600);
        agendar.setDescricao(txtAreaProcedimento.getText());
        agendar.setDataConsulta(dateChoose.getDate());
        agendar.setHora((String) comboHora.getSelectedItem());
    }
    
    private void salvarConsulta() {
        if (validarCampos()) {
            carregarDadosConsulta();
            if (agendar.getId() == null) {
                if (controllerAgendarConsulta.salvarConsulta(agendar)) {
                    limparCampos();
                }
            } else {
                controllerAgendarConsulta.alterar(agendar);
                view = new ViewConsultarConsultasGuia(login);
                view.setVisible(true);
                this.setVisible(false);
            }
            
        }
    }
    
    protected void setModel(AgendarConsulta a) {
        this.agendar = a;
        comboDentista.setSelectedItem(agendar.getDentista());
        comboPaciente.setSelectedItem(agendar.getPaciente());
        comboHora.setSelectedItem(agendar.getHora());
        dateChoose.setDate(agendar.getDataConsulta());
        txtAreaProcedimento.setText(agendar.getDescricao());
        comboDentista.setEnabled(false);
        comboPaciente.setEnabled(false);
        btnExcluir.setEnabled(true);
    }
    
    private void carregarComboHora() {
        List<String> listaHora = new ArrayList<>();
        listaHora = Arrays.asList("08:00", "09:00", "10:00", "11:00",
                "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00");
        comboHora.removeAllItems();
        comboHora.addItem("Selecione");
        for (String hora : listaHora) {
            comboHora.addItem(hora);
            
        }
    }
    
    private void limparCampos() {
        txtAreaProcedimento.setText("");
        comboDentista.setSelectedItem(null);
        comboPaciente.setSelectedItem(null);
        comboHora.setSelectedItem(null);
        dateChoose.setDate(null);
        carregarComboDentista();
        carregarComboPaciente();
        carregarComboHora();
        
    }
    
    private boolean validarCampos() {
        boolean isValido = true;
        
        if (txtAreaProcedimento.getText().equals("")) {
            isValido = false;
            MensagemUtil.msgPadrao("Procidimento");
            
        }
        if (comboHora.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Hora");
        }
        
        if (dateChoose.getDate() == null) {
            isValido = false;
            MensagemUtil.msgPadrao("Data Consulta");
        }
        
        if (comboDentista.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Dentista");
        }
        
        if (comboPaciente.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Paciente");
        }
        
        return isValido;
    }
    
    private void carregarComboPaciente() {
        List<Paciente> pa = controllerPaciente.listarTodos();
        comboPaciente.removeAllItems();
        comboPaciente.addItem("Selecione");
        for (Paciente p : pa) {
            comboPaciente.addItem(p);
        }
    }
    
    private void carregarComboDentista() {
        List<Dentista> den = controllerDentista.listarTodos();
        comboDentista.removeAllItems();
        comboDentista.addItem("Selecione");
        for (Dentista d : den) {
            comboDentista.addItem(d);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAprovar;
    private javax.swing.JToggleButton btnConsultar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnSair;
    private javax.swing.JComboBox comboDentista;
    private javax.swing.JComboBox comboHora;
    private javax.swing.JComboBox comboPaciente;
    private com.toedter.calendar.JDateChooser dateChoose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDataConsulta;
    private javax.swing.JLabel lblHoraConsulta;
    private javax.swing.JLabel lblLogado;
    private javax.swing.JLabel lblusuarioLogado;
    private javax.swing.JTextArea txtAreaProcedimento;
    // End of variables declaration//GEN-END:variables
}
